<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" >
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="账号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option v-for="item in optionTwo" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="resetting()">重置</el-button>
        <!-- <el-button v-if="isAuth('course:coursetype:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
      <el-form-item>
        <!-- <el-button type="primary" @click="jobBtn()">职务管理</el-button> -->
        <el-button type="primary" @click="addOrUpdateHandle()" v-if="isAuth('sys:user:add')">创建员工</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="phoneNumber" header-align="center" align="center" label="手机号码">
      </el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="姓名">
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{
            getStrTime(scope.row.createTime)
          }}</span>
        </template>

      </el-table-column>
      <el-table-column prop="account" header-align="center" align="center" label="创建账号">
      </el-table-column>
      <el-table-column prop="sjbAccount" header-align="center" align="center" label="世界波账号">
        <template slot-scope="scope">
          <div v-if="scope.row">
            <el-button type="text" size="small" v-if="scope.row.sjbAccountCount" @click="viewSjbAccount(scope.row)">{{ scope.row.sjbAccountCount}}</el-button>
            <span v-else>{{ scope.row.sjbAccountCount }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="perms" header-align="center" align="center" label="职位">
        <template slot-scope="scope">
          <span v-for="(item, index) in scope.row.roles" :key="index"><span v-if="index">,</span>{{
            item.roleName
          }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="loginTime" header-align="center" align="center" label="最后登录时间">
        <template slot-scope="scope">
          <span>{{
            scope.row.loginTime ? getStrTime(scope.row.loginTime) : '-'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <span>{{
            scope.row.status == 1 ? '启用' : '禁用'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="200" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="uStatus(scope.row.userId, 0)"
            v-if="scope.row.status == 1 && isAuth('sys:user:disable')">禁用</el-button>
          <el-button type="text" size="small" @click="uStatus(scope.row.userId, 1)"
            v-if="scope.row.status == 0 && isAuth('sys:user:enable')">启用</el-button>
          <el-button v-if="isAuth('sys:user:update')" type="text" size="small"
            @click="addOrUpdateHandle(scope.row.userId)">编辑</el-button>
          <el-button v-if="isAuth('sys:user:password:update')" type="text" size="small"
            @click="upwd(scope.row.userId)">修改密码</el-button>
          <el-button type="text" size="small" v-if="isAuth('sys:user:delete')"
            @click="deleteHandle(scope.row.userId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <StaffAdmin v-if="addOrUpdateVisibles" ref="staffAdmin" @refreshDataList="getDataList"></StaffAdmin>
    <UpPasswordPop v-if="ispasswordPop" ref="passwordPop" @refreshDataList="getDataList"></UpPasswordPop>
    <el-dialog title="邀请码 / 世界波账号" :visible.sync="dialogVisible3" width="30%" :before-close="handleClose3">
      <div class="drCodeCont" v-for="(item,index) in info.sjbAccountList" :key="index">
        {{item.promotionCode}} / {{item.sjbAccount}}
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible3 = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from './staff-add-or-update'
import StaffAdmin from './staffAdmin'
import UpPasswordPop from './upPasswordPop'
export default {
  data () {
    return {
      dialogVisible3: false,
      ispasswordPop: false,
      dataForm: {
        key: '',
        status: null,
        name: ''
      },
      info: {},
      options: [
        {
          value: null,
          label: '全部'
        },
        {
          value: true,
          label: '1'
        },
        {
          value: false,
          label: '2'
        },
        {
          value: false,
          label: '3'
        }
      ],
      optionTwo: [
        {
          value: null,
          label: '全部'
        },
        {
          value: 1,
          label: '启用'
        },
        {
          value: 0,
          label: '禁用'
        }
      ],
      addOrUpdateVisibles: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    StaffAdmin,
    UpPasswordPop
  },
  activated () {
    this.getDataList()
  },
  methods: {
    handleClose3 () {
      this.dialogVisible3 = false
    },
    viewSjbAccount (item) {
      this.info = item
      this.dialogVisible3 = true
    },
    uStatus (id, e) {
      this.$confirm(
        `确定要 ${e == 1
          ? '启用？'
          : '禁用？'
        }`,
        `员工${e == 1 ? '启用' : '禁用'} `,
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'danger'
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl(e == 1 ? '/admin/sys/user/enable' : '/admin/sys/user/disable'),
          method: 'post',
          data: this.$http.adornData({ userId: id, status: e }, false)
        }).then(({ data }) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
                this.dialogVisible = false
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    resetting () {
      this.dataForm = {
        key: '',
        status: null,
        name: ''
      }
      this.getDataList()
    },
    // 获取数据列表
    getDataList () {
      this.$http({
        url: this.$http.adornUrl(`/admin/sys/user/page`),
        method: 'post',
        data: this.$http.adornData({
          pageNum: this.pageIndex,
          pageSize: this.pageSize,
          name: this.dataForm.name,
          phoneNumber: this.dataForm.key,
          status: this.dataForm.status
        })
      }).then(({ data }) => {
        if (data && data.code === '200') {
          this.dataList = data.list
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
        this.$refs.addOrUpdate.getRoleList()
        this.$refs.addOrUpdate.getJobList()
      })
    },
    jobBtn () {
      this.addOrUpdateVisibles = true
      this.$nextTick(() => {
        this.$refs.staffAdmin.getJobList()
      })
    },
    upwd (id) {
      this.ispasswordPop = true
      this.$nextTick(() => {
        this.$refs.passwordPop.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      // var ids = id
      //   ? [id]
      //   : this.dataListSelections.map((item) => {
      //       return item.id;
      //     });
      this.$confirm(
        `确定对删除该账号`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl('/admin/sys/user/delete?userId=' + id),
          method: 'post',
          data: this.$http.adornData({
            userId: id
          })
        }).then(({ data }) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
